﻿#include "ToolBarZoom.h"
#include "DisplayWidget.h"
#include <QDebug>
#include <QLabel>
#include <QSpinBox>
#include <QCheckBox>

namespace Qly {

ToolBarZoom::ToolBarZoom(const QString &title, QWidget *parent)
    :QToolBar(title, parent)
{
    setIconSize(QSize(32, 32));
    initContent();
    centerWidget = nullptr;
}

void ToolBarZoom::setZoomFactor(double f)
{
    int n = qRound(f * 100.0);
    //qDebug() << "ToolBarZoom::setZoomFactor = " << f << ", n = " << n;
    spinBoxZoom->setValue(n);
}

void ToolBarZoom::loadSettings(QSettings &settings)
{
    int zoom = settings.value("Display/ZoomFactor", 70).toInt();
    double rotate = settings.value("Display/Rotate", 0).toDouble();
    bool cb = settings.value("Display/ShowCross", false).toBool();

    spinBoxZoom->setValue(zoom);
    spinBoxRotate->setValue(rotate);
    checkBox->setChecked(cb);
}
void ToolBarZoom::writeSettings(QSettings &settings)
{
    int zoom = spinBoxZoom->value();
    double rotate = spinBoxRotate->value();
    bool cb = checkBox->isChecked();

    settings.setValue("Display/ZoomFactor", zoom);
    settings.setValue("Display/Rotate", rotate);
    settings.setValue("Display/ShowCross", cb);

}

void ToolBarZoom::attach(DisplayWidget *widget)
{
    centerWidget = widget;
    connect(spinBoxZoom, SIGNAL(valueChanged(int)), centerWidget, SLOT(setZoomFactorPCT(int)));
    connect(checkBox, SIGNAL(toggled(bool)), centerWidget, SLOT(showCross(bool)));
    connect(spinBoxRotate, SIGNAL(valueChanged(double)), centerWidget, SLOT(setRotate(double)));
    connect(centerWidget, SIGNAL(zoomFactorChanged(double)), this, SLOT(setZoomFactor(double)));
    spinBoxZoom->setValue(70);
    //checkBox->setChecked(false);
}

void ToolBarZoom::initContent()
{
    setWindowTitle("Image Zoom Toolbar");
    setObjectName(QStringLiteral("ToolBarZoom"));

    QLabel *label = new QLabel(tr("Zoom Factor: "), this);
    addWidget(label);

    spinBoxZoom = new QSpinBox;
    spinBoxZoom->setSuffix(" %");
    spinBoxZoom->setRange(10, 1000);
    spinBoxZoom->setValue(100);
    spinBoxZoom->setSingleStep(1);
    spinBoxZoom->setFixedWidth(80);
    addWidget(spinBoxZoom) ;
    addSeparator();
    checkBox = new QCheckBox(QStringLiteral("+"));
    addWidget(checkBox) ;
    checkBox->setChecked(false);

    spinBoxRotate = new QDoubleSpinBox(this);
    spinBoxRotate->setRange(-90, 90);
    spinBoxRotate->setDecimals(1);
    spinBoxRotate->setSingleStep(0.5);
    addWidget(spinBoxRotate);
}

ToolBarZoom::ToolBarZoom(QWidget *parent)
    :QToolBar(parent)
{
    setIconSize(QSize(32, 32));

    initContent();
    centerWidget = nullptr;
}

} //namespace Qly
